# -*-coding:utf-8-*-
import os

from logTest import logger


def is_number(num):
    while num.isdigit() == True:
        num = int(num)
        break
    else:
        num = input("\n输入的数据类型不对，请重新输入值（要求整数） ：")
        is_number(num)
    return num


# 获取设备名
def get_devices():
    devicelist = list()
    devices = os.popen("adb devices").readlines()
    if len(devices) == 1 or len(devices) == 2 or '* daemon not running. starting it now on port 5037 *\n' in devices :
        os.system("adb devices")
        print("\n未连接设备，请重试")
    else:
        for device in devices:
            if "\tdevice\n" in device:
                deviceA = str(device).split("\tdevice\n")[0]
                devicelist.append(deviceA)
    # print(devicelist)
    logger.info("当前正在运行的设备devices有:{}".format(devicelist))
    return devicelist


# 获取包名
def get_package():
    devices = get_devices()
    dpall = list()
    for device in devices:
        dp = list()
        dp.append(device)
        package = os.popen('adb -s {} shell dumpsys activity top|find "ACTIVITY"'.format(device)).readlines()
        for packname in package:
            packname = packname.strip().split("/")[0]
            if 'ACTIVITY' in packname:
                packname = packname[9:]
                dp.append(packname)
            dpall.append(dp)
    logger.info("当前正在运行的设备及包名分别有:{}".format(dpall))
    return dpall
